package com.seasugar.raggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seasugar.raggie.common.BaseContext;
import com.seasugar.raggie.common.R;
import com.seasugar.raggie.model.entity.Orders;
import com.seasugar.raggie.model.vo.OrderVo;
import com.seasugar.raggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("page")
    public R page(Integer page, Integer pageSize, Long number, String beginTime, String endTime) {
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Orders::getOrderTime);
        queryWrapper.like(number != null, Orders::getNumber, number);
        queryWrapper.between(beginTime != null && endTime != null, Orders::getOrderTime, beginTime, endTime);
        log.info("页数:{},条数:{}", page, pageSize);
        Page pageInfo = new Page(page, pageSize);
        orderService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }

//    @CachePut(value = "orderCache",key = "'orders'")
//    @CacheEvict(value = "{orderCache,shoppingCartCache}",allEntries = true)
    @Caching(evict = {
            @CacheEvict(value = "orderCache",allEntries = true),
            @CacheEvict(value = "shoppingCartCache",allEntries = true)
    })
    @PostMapping("submit")
    public R submitOrder(@RequestBody Orders orders, HttpSession session) {
        orderService.submit(orders, session);
        return R.success("下单成功");
    }

    @Cacheable(value = "orderCache",key = "#root.methodName")
    @GetMapping("userPage")
    public R orders(Integer page, Integer pageSize) {
        Long currentId = BaseContext.getCurrentId();
        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Orders::getOrderTime);
        queryWrapper.eq(Orders::getUserId,currentId);
        orderService.page(pageInfo, queryWrapper);
//        LambdaQueryWrapper<Orders> queryWrapper=new LambdaQueryWrapper<>();
//        queryWrapper.eq(Orders::getUserId,userId);
//        //以付款时间降序排列
//        queryWrapper.orderByDesc(Orders::getCheckoutTime);
        Page<OrderVo> res = orderService.getPage(pageInfo);
        return R.success(res);
    }

    @PutMapping
    public R distribute(@RequestBody Orders orders) {
        //update可以根据选择更新，只不过必须要有id
        boolean b = orderService.updateById(orders);
        if (b)
            return R.success("修改配送状态成功");
        else
            return R.error("未找到");
    }
}
